﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using Libs.HR;

public partial class Pages_Hr_Employees_Birthday : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Danh sách sinh nhật";
        if (!IsPostBack)
        {
            init();
            BindData();
        }
    }

    protected string ImgUrl(string id)
    {
        return Constant.ADMIN_PATH + Resources.Url.FileDataView + "?id=" + id;
    }

    private void init()
    {
        // Department
        var _Department = new Departments();
        drpDepartment.DataSource = _Department.GetTList();
        drpDepartment.DataBind();
        drpDepartment.Items.Insert(0, new ListItem("Chọn phòng ban:", "0"));

        // Đặt ngày
        txtDateMin.Text = "01" + DateTime.Today.ToString("/MM");
        txtDateMax.Text = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(1).AddDays(-1).ToString("dd/MM");
    }

    private void BindData()
    {
        var _Employee = new Employees();
        int total = 0;
        int pageSize = Convert.ToInt32(drpPageSize.SelectedValue);
        int pageIndex = Convert.ToInt32(drpPage.SelectedValue);
        int departmentID = Convert.ToInt32(drpDepartment.SelectedValue);

        DateTime dateMin = DateTime.Today;
        try
        {
            dateMin = Convert.ToDateTime(txtDateMin.Text + "/" + DateTime.Today.Year.ToString(), new CultureInfo("vi-VN"));
        }
        catch
        {
            Message.Alert(this.Page, "Ngày bắt đầu không đúng!");
            return;
        }

        DateTime dateMax = DateTime.Today;
        try
        {
            dateMax = Convert.ToDateTime(txtDateMax.Text + "/" + DateTime.Today.Year.ToString(), new CultureInfo("vi-VN"));
        }
        catch
        {
            Message.Alert(this.Page, "Ngày kết thúc không đúng!");
            return;
        }

        if (dateMin > dateMax)
        {
            Message.Alert(this.Page, "Ngày bắt đầu lớn hơn ngày kết thúc!");
            return;
        }

        rptList.DataSource = _Employee.GetTListBirthday(departmentID, dateMin, dateMax, pageIndex, pageSize, ref total);
        rptList.DataBind();

        lblTotal.Text = "(" + total.ToString() + ")";
        total = (total - 1) / pageSize + 1;
        if (total == 0) total = 1;
        drpPage.Items.Clear();
        for (int i = 1; i <= total; i++)
        {
            drpPage.Items.Add(new ListItem(i.ToString()));
        }

        try
        {
            drpPage.SelectedValue = pageIndex.ToString();
        }
        catch
        {
        }
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesAdd);
    }

    protected void btPage_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btView_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
